package ysq.xyg.recovery.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.extend.ehcache.EhCacheService;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.recovery.dao.RecoveryLargeCatDao;
import ysq.xyg.recovery.model.po.RecoveryLargeCat;
import ysq.xyg.recovery.model.po.RecoveryMiddleCat;
import ysq.xyg.recovery.model.vo.RecoveryLargeCatVo;
import ysq.xyg.recovery.service.RecoveryLargeCatService;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysDictContentVo;
import ysq.xyg.sys.service.SysDictContentService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 回收金模块-大分类service实现
 *
 * @author congcong tao
 * @date 2019-5-8 15:00:56
 */
@Service("recoveryLargeCatService")
public class RecoveryLargeCatServiceImpl extends BaseServiceImpl<RecoveryLargeCat> implements RecoveryLargeCatService {

    @Autowired
    private SysDictContentService sysDictContentService;
    @Autowired
    private EhCacheService ehCacheService;
    @Resource
    private RecoveryLargeCatDao recoveryLargeCatDao;

    private RecoveryLargeCat recoveryLargeCat;

    @Autowired
    public void setBaseDao(RecoveryLargeCatDao recoveryLargeCatDao) {
        this.baseDao = recoveryLargeCatDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryLargeCatVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:00:56
     */
    @Override
    public ReturnEntity page(RecoveryLargeCatVo recoveryLargeCatVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryLargeCat> list = acceptList(recoveryLargeCatVo);
        PageInfo<RecoveryLargeCat> page=new PageInfo<RecoveryLargeCat>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryLargeCatVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:00:56
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryLargeCatVo recoveryLargeCatVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryLargeCatVo.getId())){
            recoveryLargeCat=new RecoveryLargeCat(currentUser);
            isUpate=false;
        }else{
            recoveryLargeCat=selectByPrimaryKey(recoveryLargeCatVo.getId());
            if(ObjectUtil.isNull(recoveryLargeCat)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryLargeCat.setUpdateTime(new Date());
            recoveryLargeCat.setUpdateUser(currentUser.getId());
        }
        recoveryLargeCat.setName(recoveryLargeCatVo.getName());
        recoveryLargeCat.setIcon(recoveryLargeCatVo.getIcon());
        recoveryLargeCat.setDescription(recoveryLargeCatVo.getDescription());
        recoveryLargeCat.setOrderNum(recoveryLargeCatVo.getOrderNum());
        recoveryLargeCat.setStatus(recoveryLargeCatVo.getStatus());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryLargeCat);
        }else{
            num=insert(recoveryLargeCat);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        // 更新缓存
        ehCacheService.evictRecoveryCat();
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-5-8 15:00:56
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryLargeCat recoveryLargeCat=selectByPrimaryKey(id);
        if(recoveryLargeCat!=null){
            recoveryLargeCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryLargeCat.setUpdateTime(new Date());
            recoveryLargeCat.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryLargeCat);
            if (num > 0) {
                // 更新缓存
                ehCacheService.evictRecoveryCat();
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryLargeCatVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-5-8 15:00:56
     */
    public List<RecoveryLargeCat> acceptList(RecoveryLargeCatVo recoveryLargeCatVo){
        Example example = new Example(RecoveryLargeCat.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryLargeCatVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryLargeCatVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(recoveryLargeCatVo.getName())){
            criteria.andLike("name","%" + recoveryLargeCatVo.getName() + "%");
        }
        example.setOrderByClause(" update_time desc");
        return recoveryLargeCatDao.selectByExample(example);
    }

    @Override
    public ReturnEntity acceptSelect(String code) {
        List<RecoveryLargeCat> list = acceptList(new RecoveryLargeCatVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(RecoveryLargeCat entity : list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(entity.getId());
            sysDictContent.setName(entity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(entity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }

    @Override
    public ReturnEntity dTreeList(RecoveryLargeCatVo recoveryLargeCatVo) {
        List<RecoveryLargeCat> list = acceptList(recoveryLargeCatVo);
        List<Map<String,Object>> dtreeEntityList = new ArrayList<Map<String,Object>>();
        List<SysDictContent> dictList = sysDictContentService.acceptList(new SysDictContentVo("unit_type",BaseConstant.BASE_STATUS_NORMAL));
        Map<String, List<RecoveryMiddleCat>> map = ehCacheService.selectRecoveryMiddleCatList();
        for (RecoveryLargeCat largeCat : list) {
            Map<String,Object> dtreeEntity = new HashedMap();
            dtreeEntity.put("id", largeCat.getId());
            dtreeEntity.put("title", largeCat.getName());
            dtreeEntity.put("parentId", "0");

            Map<String,String> m = new HashedMap();
            m.put("type","0");
            m.put("isChecked","0");
            List<Map<String,String>> dtreeCheckEntityList = new ArrayList<Map<String,String>>();
            dtreeCheckEntityList.add(m);

            if (map != null && map.size() > 0) {
                for (Map.Entry<String, List<RecoveryMiddleCat>> entry : map.entrySet()) {
                    if (StringUtils.equals(entry.getKey(), largeCat.getId())) {
                        List<RecoveryMiddleCat> recoveryMiddleCatList = entry.getValue();
                        if (recoveryMiddleCatList != null && recoveryMiddleCatList.size() > 0) {
                            dtreeEntity.put("isLast", false);
                            for (RecoveryMiddleCat recoveryMiddleCat : recoveryMiddleCatList) {
                                for (SysDictContent sysDictContent : dictList) {
                                    if(sysDictContent.getValue().equals(recoveryMiddleCat.getRatePrice())){
                                        recoveryMiddleCat.setRatePriceName(sysDictContent.getName());
                                    }
                                }
                                Map<String, Object> dtreeEntityD = new HashedMap();
                                dtreeEntityD.put("id", recoveryMiddleCat.getId());
                                if (StringUtils.isNotEmpty(recoveryMiddleCat.getPrice()) && StringUtils.isNotEmpty(recoveryMiddleCat.getRatePriceName())) {
                                    dtreeEntityD.put("title", recoveryMiddleCat.getName() + "-" + recoveryMiddleCat.getPrice() + recoveryMiddleCat.getRatePriceName());
                                } else {
                                    dtreeEntityD.put("title", recoveryMiddleCat.getName());
                                }
                                dtreeEntityD.put("parentId", largeCat.getId());
                                dtreeEntityD.put("isLast", true);
                                dtreeEntityD.put("checkArr", dtreeCheckEntityList);
                                dtreeEntityList.add(dtreeEntityD);
                            }
                        } else {
                            dtreeEntity.put("isLast", true);
                        }

                    }
                }
            } else {
                dtreeEntity.put("isLast", false);
            }
            dtreeEntity.put("checkArr", dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        return new ReturnEntity(dtreeEntityList);
    }

    @Override
    public List<RecoveryLargeCat> selectRecoveryLargeCatList() {
        RecoveryLargeCat recoveryLargeCat = new RecoveryLargeCat();
        recoveryLargeCat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        return this.select(recoveryLargeCat);
    }
}
